package com.fxd.test;

import com.fxd.dao.accountDao;
import com.fxd.domain.account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

/**
 * @auther fuxudong
 * @date 2021/2/7 - 16:06
 */
public class testmabatis {

    @Test
    public void findall() throws Exception{
        //加载配置文件
        InputStream rs = Resources.getResourceAsStream("SqlMapConfig.xml");
        //创建工厂对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(rs);
        //创建SQLsession对象
        SqlSession sqlSession = factory.openSession();
        //获取代理对象
        accountDao accountDao = sqlSession.getMapper(accountDao.class);
        List<account> accounts = accountDao.findAll();
        for (account account:accounts){
            System.out.println(account);
        }
        //关闭资源
        sqlSession.close();
        rs.close();
    }

    @Test
    public void save() throws Exception{
        account account = new account();
        account.setName("a");
        account.setMoney(33d);
        //加载配置文件
        InputStream rs = Resources.getResourceAsStream("SqlMapConfig.xml");
        //创建工厂对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(rs);
        //创建SQLsession对象
        SqlSession sqlSession = factory.openSession();
        //获取代理对象
        accountDao accountDao = sqlSession.getMapper(accountDao.class);
        accountDao.saveAccount(account);
        sqlSession.commit();
        //关闭资源
        sqlSession.close();
        rs.close();
    }
}
